package com.wq;

import com.wq.code.ByteUtils;
import jdk.internal.util.xml.impl.Input;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

/**
 * @Author: wangqiang20995
 * @Date:2019/6/24
 * @Description:
 * @Resource:
 */
public class SocketClient {

    public static void main(String[] args) throws IOException {
        Socket socket = new Socket("localhost", Integer.parseInt("6650"));

        OutputStream out = socket.getOutputStream();
        byte[] data = ("<?xml version=\"1.0\" encoding=\"GB2312\"?>\n" +
                "<MsgText>\n" +
                "<MsgHdr>\n" +
                "<ServiceNo>02</ServiceNo>\n" +
                "<FunctionNo>1001</FunctionNo>\n" +
                "<BankNo>012</BankNo>\n" +
                "<RequestNo>2015072200001-$</RequestNo>\n" +
                "<PackCount>1</PackCount>\n" +
                "<OwnPackNo>8001</OwnPackNo>\n" +
                "<CompanyCode>102-$</CompanyCode>\n" +
                "<UserCode>2200003220</UserCode>\n" +
                "<Operator>2200003220001</Operator>\n" +
                "<Password>******</Password>\n" +
                "<TradeDate>2015-07-22</TradeDate>\n" +
                "<TradeTime>19:53:50</TradeTime>\n" +
                "<FuncIndex>0</FuncIndex>\n" +
                "<CertID>111111111111</CertID>\n" +
                "<AccoCashType></AccoCashType>\n" +
                "<MainSubType>0</MainSubType>\n" +
                "</MsgHdr>\n" +
                "<MsgPack>\n" +
                "<PayBranchCode></PayBranchCode>\n" +
                "<AreaCode></AreaCode>\n" +
                "<MoneyType></MoneyType>\n" +
                "<PayAcco>13020251241</PayAcco>\n" +
                "<QryBegDate>20151224</QryBegDate>\n" +
                "<QryEndDate>20151224</QryEndDate>\n" +
                "<MinBala>00000</MinBala>\n" +
                "<MaxBala>9999999</MaxBala>\n" +
                "<LoanDirect>0</LoanDirect>\n" +
                "<MainAcco>123456</MainAcco>\n" +
                "<SubAccoNo>000000</SubAccoNo>\n" +
                "<CityNo></CityNo>\n" +
                "</MsgPack>\n" +
                "\n" +
                "</MsgText>").getBytes("GB2312");
        out.write(data);
        out.flush();

        InputStream inputStream = socket.getInputStream();

        byte[] header = new byte[4];
        inputStream.read(header);

        int length = ByteUtils.unsignedByteToInt(header);
        System.out.println("length:" + length);

        byte[] buffer = new byte[1024];
        while (inputStream.read(buffer) != -1){
            String str = new String(buffer);
            System.out.println(str);
        }


    }
}
